package com.run;

import com.model.Book;

import java.sql.*;
import java.util.Objects;

public class BookRun {
    public ResultSet SEARCHBOOKS (Connection con,String book_id,String book_name) throws SQLException {
        Book resultBook = new Book();
        if(Objects.equals(book_id,"")) {
            book_id = "null";
        }
        if(Objects.equals(book_name,"")) {
            book_name = "null";
        }
        ResultSet resultSet = null;
        System.out.println(resultBook.getBook_id());
        System.out.println(resultBook.getBook_name());
        if(book_id == "null" && book_name != "null") {
            String sql = "select * from book where book_name = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,book_name);
            resultSet = pstmt.executeQuery();
//            while(resultSet.next()) {
//                if(Objects.equals(resultSet.getString("book_name"),book_name)) {
//                    resultBook.setBook_id(resultSet.getString("book_id"));
//                    resultBook.setBook_name(resultSet.getString("book_name"));
//                    resultBook.setQuantity(resultSet.getInt("quantity"));
//                    resultBook.setLentTimes(resultSet.getInt("lentTimes"));
//                    resultBook.setRemarks(resultSet.getString("remarks"));
//                    break;
//                }
//            }
        }
        else if(book_id != "null" && book_name == "null") {
            String sql = "select * from book where book_id = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, book_id);
            resultSet = pstmt.executeQuery();
        }
        else if(book_id != "null" && book_name != "null"){
            String sql = "select * from book where book_id = ? and book_name = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, book_id);
            pstmt.setString(2,book_name);
            resultSet = pstmt.executeQuery();
        }
//        else {
//            String sql = "select * from book";
//            PreparedStatement pstmt = con.prepareStatement(sql);
//            resultSet = pstmt.executeQuery();
//        }

        return resultSet;
    }




    public ResultSet searchBook (Connection con,String book_id,String book_name) throws SQLException {
        Book resultBook = new Book();
        if(Objects.equals(book_id,null)) {
            book_id = "null";
        }
        if(Objects.equals(book_name,null)) {
            book_name = "null";
        }
        ResultSet resultSet = null;
        System.out.println(resultBook.getBook_id());
        System.out.println(resultBook.getBook_name());
        if(book_id == "null" && book_name != "null") {
            String sql = "select * from book where book_name = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,book_name);
            resultSet = pstmt.executeQuery();
//            while(resultSet.next()) {
//                if(Objects.equals(resultSet.getString("book_name"),book_name)) {
//                    resultBook.setBook_id(resultSet.getString("book_id"));
//                    resultBook.setBook_name(resultSet.getString("book_name"));
//                    resultBook.setQuantity(resultSet.getInt("quantity"));
//                    resultBook.setLentTimes(resultSet.getInt("lentTimes"));
//                    resultBook.setRemarks(resultSet.getString("remarks"));
//                    break;
//                }
//            }
        }
        else if(book_id != "null" && book_name == "null") {
            String sql = "select * from book where book_id = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, book_id);
            resultSet = pstmt.executeQuery();
        }
        else if(book_id != "null" && book_name != "null"){
            String sql = "select * from book where book_id = ? and book_name = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, book_id);
            pstmt.setString(2,book_name);
            resultSet = pstmt.executeQuery();
        }
//        else {
//            String sql = "select * from book";
//            PreparedStatement pstmt = con.prepareStatement(sql);
//            resultSet = pstmt.executeQuery();
//        }

        return resultSet;
    }

    public boolean borrowBook(Connection con, String student_id, String book_id, String outdate) throws SQLException {
        String updateRecords = "INSERT INTO records values (null,'" + student_id +"','" + book_id + "','" + outdate + "',null)";
        String updateBook = "UPDATE book SET lentTimes = lentTimes+1 WHERE book_id = '" + book_id + "'";
        Statement stmt = con.createStatement();
        int count = 0;
        if(stmt.executeUpdate(updateRecords) == 1) {
            count++;
        }
        if(stmt.executeUpdate(updateBook) == 1) {
            count++;
        }
        if(count == 2) {
            System.out.println("借书成功");
            return true;
        }
        else {
            System.out.println("借书失败");
            return false;
        }
    }


    public boolean returnBook(Connection con, String student_id, String book_id, String backdate) throws SQLException {
        String updateRecords = "UPDATE records SET backDate = '" + backdate +"' WHERE student_id = '" + student_id + "' and book_id = '" + book_id + "'";
        Statement stmt = con.createStatement();
        int count = 0;
        if(stmt.executeUpdate(updateRecords) == 1) {
            count++;
        }
        if(count == 1) {
            System.out.println("还书成功");
            return true;
        }
        else {
            System.out.println("还书失败");
            return false;
        }
    }
}
